<?php

class QQAuth_Util
{
	/**
	 * 构造url的参数串
	 * 
	 * @param $params array 参数数组
	 * 
	 * @return $args string 参数串
	 * 传递 $params = array(
	 * 					'name'	=> 'admin',
	 * 					'age' 	=> '15'
	 * 					);
	 * 
	 * 返回 name=admin&age=15
	 * 
	 */
	
	public static function composeParams( $params = array())
	{
		$args = array();
		
		if ( is_array($params) )
		{ 
			foreach ($params as $key => $value)	
			{
				$args[] = $key . '=' . $value;
			}
		}
		
		return implode( '&', $args );
	}
	
	/**
	 * 判断是否字符串是否以某个字符串开头
	 * 
	 * @param $hayback string 待搜索字符串
	 * @param $needle string 开头字符串
	 * 
	 * @return boolean 如果$hayback以$needle开头，则返回true，否则返回false.
	 * 
	 * 
	 */
	public static function startsWith( $hayback, $needle )
	{
		if ( strpos( $hayback, $needle ) === 0 )
		{
			return true;
		}
		return false;
	}
	
	public static function doPost( $url, $data )
	{
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
	    curl_setopt($ch, CURLOPT_POST, TRUE); 
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	    curl_setopt($ch, CURLOPT_URL, $url);
	    $ret = curl_exec($ch);
	
	    curl_close($ch);
	    return $ret;
	}
	
	public static function doGet( $url )
	{
	    if (ini_get("allow_url_fopen") == "1")
	        return file_get_contents($url);
	
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	    curl_setopt($ch, CURLOPT_URL, $url);
	    $result =  curl_exec($ch);
	    curl_close($ch);
	
	    return $result;
	}
	
	public static function allSet()
	{
		foreach ( func_get_args() as $value)
		{
			if( empty($value) || $value == '')
			{
				return false;
			}
		}
		return true;

	}
	
	public static function parseJSONP( $jsonp , $callback = "callback")
	{
		if (strpos($jsonp, $callback) !== false)
	    {
	        $lpos = strpos($jsonp, "(");
	        $rpos = strrpos($jsonp, ")");
	        $str  = substr($jsonp, $lpos + 1, $rpos - $lpos -1);
	    }
	   	return json_decode($str);

	}
}